﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class UserBLL
  {
    //Get all
    public List<User> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.OrderByDescending(p => p.Id).ToList();
      }
    }

    public List<User> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.isEnable == isEnable).OrderByDescending(p => p.Id).ToList();
      }
    }

    //Get by Role Id

    public List<User> GetByRoleId(int roleId)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.RoleId == roleId).OrderByDescending(p => p.Id).ToList();
      }
    }

    public List<User> GetByRoleId(int roleId, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.RoleId == roleId && p.isEnable == isEnable).OrderByDescending(p => p.Id).ToList();
      }
    }

    //Get by Id

    public User GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public User GetById(int id, bool isEnable)
    {
      using (var data =new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    public User GetByUserName(string userName)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.UserName == userName && p.isEnable == true).FirstOrDefault();
      }
    }

    public User GetByEmail(string email)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Users.Where(p => p.Email == email && p.isEnable == true).FirstOrDefault();
        }
    }

    public string GetByStringEmail(string email)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            if (email == "")
                return "";
            else
                return data.Users.Where(p => p.Email == email).FirstOrDefault().Email;
        }
    }

    public User GetByEmails(string email)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Users.Where(p => p.Email == email).FirstOrDefault();
      }
    }

    public int GetMaxId()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        DAL.User obj = data.Users.OrderByDescending(p => p.Id).FirstOrDefault();
        return obj == null ? 1 : (obj.Id + 1);
      }
    }

    public string GetTotalBuy(int id)
    {
      OrderBLL orderData = new OrderBLL();
      OrderDetailsBLL orderDetailData = new OrderDetailsBLL();

      decimal total = 0;
      var listOrder = orderData.GetByUserId(id);
      foreach (var item in listOrder)
      {
        var listDetails = orderDetailData.GetByOrderId(item.Id);
        foreach (var item_Details in listDetails)
        {
          total += item_Details.Cost == null ? 0 : (item_Details.Cost.Value * item_Details.Count);
        }
      }
      return String.Format("{0:0,0}", total);
    }
    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User user = data.Users.Where(p => p.Id == id).FirstOrDefault();
        user.isEnable = !user.isEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User user = data.Users.Where(p => p.Id == id).FirstOrDefault();
        user.isEnable = isEnable;

        data.SubmitChanges();
      }
    }


    //Check exist user

    public bool isExitsUserName(string userName)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User obj = data.Users.Where(p => p.UserName == userName).FirstOrDefault();
        if (obj == null) return false;
        else return true;
      }
    }

    //Check exist Email

    public bool isExitsEmail(string email)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User obj = data.Users.Where(p => p.Email == email).FirstOrDefault();
        if (obj == null) return false;
        else return true;
      }
    }

    //Check login

    public bool isCheckLoginUserName(string username, string pass)
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            User obj = data.Users.Where(p => p.UserName == username && p.PassWord == pass).FirstOrDefault();
            if (obj == null) return false;
            else return true;
        }
    }

    public bool isCheckLogin(string email, string pass)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User obj = data.Users.Where(p => p.Email == email && p.PassWord == pass).FirstOrDefault();
        if (obj == null) return false;
        else return true;
      }
    }

    //Change pass

    public void ChangePass(string userName, string newPass)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User user = data.Users.Where(p => p.UserName == userName).FirstOrDefault();
        user.PassWord = newPass;

        data.SubmitChanges();
      }
    }

    //Add

    public void Add(User obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Users.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    //Update

    public void Update(User obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User user = data.Users.Where(p => p.Id == obj.Id).FirstOrDefault();
        user.CreatedDate = obj.CreatedDate;
        user.Email = obj.Email;
        user.isEnable = obj.isEnable;
        user.Name = obj.Name;
        user.PassWord = obj.PassWord;
        user.Phone = obj.Phone;
        user.RoleId = obj.RoleId;
        user.UserName = obj.UserName;
        user.Avatar = obj.Avatar;
        user.Address = obj.Address;

        data.SubmitChanges();
      }
    }

    //Delete

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        User obj = data.Users.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
          data.Users.DeleteOnSubmit(obj);

        data.SubmitChanges();
      }
    }
  }
}
